package com.example.testdemo.demos.web.Controller;

import com.example.testdemo.demos.web.Config.JwtInfo;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Entity.Goods;
import com.example.testdemo.demos.web.Entity.Merchant;
import com.example.testdemo.demos.web.Service.GoodsService;
import com.example.testdemo.demos.web.Service.JwtRedistService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private JwtRedistService jwtRedistService;

    /**
     * 新增商品
     */
    @PostMapping("/addGoods")
    public Result addMerchant(@RequestBody Goods goods, @RequestHeader("token") String token) {
        Result result = new Result();
        try {
            JwtInfo jwtInfo = new JwtInfo();
            if (!(token.isEmpty())) {
                if (!(goods.getCategory() ==null&&!(goods.getGoodsName() ==null))){
                    jwtInfo = jwtRedistService.getUserInfo(token);
                    return goodsService.addGoods(goods, jwtInfo);
                }else {
                    result.setCode(ResultEnum.BADREQUEST.getCode());
                    result.setMsg("请求参数不足或不合法！");
                }
            } else {
                jwtInfo.setPass(false);
            }
        } catch (Exception e) {
            result.setCode(ResultEnum.FORBIDDEN.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
        }
        return result;
    }

}
